import { ActionTree, ActionContext } from 'vuex';
import { PermissionState } from './state';
import { RootState } from '@/store';
import { Mutations } from './mutations';
import { PermissionActionType, PermissionMutationType } from './types';
import { asyncRoutes } from '@/router';

type AugmentedActionContext = {
  commit<K extends keyof Mutations>(
    key: K,
    payload: Parameters<Mutations[K]>[1],
  ): ReturnType<Mutations[K]>
} & Omit<ActionContext<PermissionState, RootState>, 'commit'>;

export interface Actions {
  [PermissionActionType.ACTION_SET_ROUTES](
    { commit }: AugmentedActionContext,
    roles?: string[],
  ): void,
}

export const actions: ActionTree<PermissionState, RootState> & Actions = {
  [PermissionActionType.ACTION_SET_ROUTES]({ commit }: AugmentedActionContext) {
    const accessedRoutes = asyncRoutes;
    commit(PermissionMutationType.SET_ROUTES, accessedRoutes);
  },
};
